package org.example.myleet.p1640;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public boolean canFormArray(int[] arr, int[][] pieces) {
        Map<Integer, int[]> startNumberMapPiece = new HashMap<>(pieces.length);
        for (int[] piece : pieces) {
            startNumberMapPiece.put(piece[0], piece);
        }
        for (int i = 0; i < arr.length; ++i) {
            int[] piece = startNumberMapPiece.get(arr[i]);
            if (null == piece) {
                return false;
            }
            for (int j = 0; j < piece.length; ++j) {
                if (piece[j] == arr[i]) {
                    ++i;
                } else {
                    return false;
                }
            }
            --i;
        }
        return true;
    }
}
